import { constantRoutes, asyncRouterMap } from "@/router";

const permission = {
  state: {
    routes: constantRoutes,
    addRoutes: asyncRouterMap,
    defaultRoutes: [],
    topbarRouters: [],
    sidebarRouters: [],
  },
  mutations: {
    SET_ROUTES: (state, routes) => {
      state.addRoutes = routes;
      state.routes = constantRoutes.concat(routes);
    },
    SET_DEFAULT_ROUTES: (state, routes) => {
      state.defaultRoutes = constantRoutes.concat(routes);
    },
    SET_TOPBAR_ROUTES: (state, routes) => {
      state.topbarRouters = routes;
    },
    SET_SIDEBAR_ROUTERS: (state, routes) => {
      state.sidebarRouters = routes;
    },
  },
  actions: {
    GenerateRoutes({ commit }, data) {
      return new Promise((resolve) => {
        const { perms } = data;
        let accessedRouters;
        if (perms.includes("*")) {
          accessedRouters = asyncRouterMap;
        }
        // else {
        //   accessedRouters = filterAsyncRouter(asyncRouterMap, perms)
        // }
        commit("SET_ROUTES", accessedRouters);
        resolve();
      });
    },
  },
};

export default permission;
